//
//  OMPCUserInfoBar.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/3/4.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMPCUserInfoBar.h"
#import "UIImage+ImageEffects.h"
@interface OMPCUserInfoBar ()
@property (nonatomic, strong) UILabel *nameLabel;           //用户名标签
@property (nonatomic, strong) UILabel *levelLabel;          //等级标签
@property (nonatomic, strong) UIImageView *avatarImageView; //用户头像容器
@property (nonatomic, strong) UIImageView *levelImageView;  //等级图片容器
@property (nonatomic, strong) UIImageView *blurImageView;   //高斯模糊
@property (nonatomic, strong) UIButton *toLoginButton;      //去登录的按钮
@end

@implementation OMPCUserInfoBar

+ (instancetype)customeInfoBar
{
    OMPCUserInfoBar *bar = [OMPCUserInfoBar new];
    [bar configure];
    return bar;
}

#define kIconWidth SM_SIZE(140)
- (void)configure
{
    [self setupUI];

    @weakify(self);
    //登录监听
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGIN_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
    }];

    //登出监听
    [SMNotificationMonitorRACSignal(OM_NOTIFICATION_KEY_LOGOUT_SUCCESS) subscribeNext:^(id x) {
        @strongify(self);
        [self reloadData];
    }];

    [self reloadData];
}


- (void)reloadData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        OMAccount *account = KTSystemStorageSingleton.account;
        dispatch_async(dispatch_get_main_queue(), ^{

            if (account) {
                [self loadUIWithAccount:account];

            }else {
                [self loadUIWithoutAccount];
            }
        });
    });
}

- (void)loadUIWithAccount:(OMAccount *)account
{
    @weakify(self);
    [self.avatarImageView kt_imageWithURLStr:account.avatar
                                 placeHolder:[UIImage st_imageNamed:@"ic_personal_avatar_placehold.png" type:@""]
                                    withSize:CGSizeMake(kIconWidth*3, kIconWidth*3)
                                    progress:nil
                                onCompletion:^(UIImage * _Nullable image, BOOL isSuccess) {
                                    @strongify(self);
                                    [self reloadBlurImageView];
                                }];
    _toLoginButton.hidden = YES;
    _levelLabel.hidden = NO;
    _nameLabel.text = account.username;
}

- (void)loadUIWithoutAccount
{
    _toLoginButton.hidden = NO;
    _levelLabel.hidden = YES;
    _nameLabel.text = @"未登录";
    _avatarImageView.image = [UIImage st_imageNamed:@"ic_personal_avatar_placehold.png" type:@""];
    [self reloadBlurImageView];
}


- (void)reloadBlurImageView
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [_avatarImageView.image applyBlurWithRadius:5 tintColor:[UIColor colorWithWhite:.7 alpha:0.6] saturationDeltaFactor:1.0 maskImage:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.blurImageView setImage:image];
        });
    });
}

- (void)setupUI
{
    CGRect frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(270));
    [self setFrame:frame];

    _blurImageView = [UIImageView new];
    _blurImageView.contentMode = UIViewContentModeScaleAspectFill;
    _blurImageView.clipsToBounds = YES;
    [self addSubview:_blurImageView];
    [_blurImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];

    _avatarImageView = [UIImageView new];
    _avatarImageView.clipsToBounds = YES;
    _avatarImageView.layer.cornerRadius = kIconWidth/2;
    [self addSubview:_avatarImageView];
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(45)));
        make.centerY.equalTo(self.mas_centerY);
        make.width.height.equalTo(@(kIconWidth));
    }];

    _nameLabel = [UILabel new];
    _nameLabel.font = SM_CFont(35);
    _nameLabel.textColor = SM_whiteColor;
    [self addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarImageView.mas_right).offset(SM_SIZE(30));
        make.bottom.equalTo(_avatarImageView.mas_centerY).offset(SM_SIZE(-10));
    }];

    _levelImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_personal_star"]];
    [self addSubview:_levelImageView];
    [_levelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_left);
        make.top.equalTo(self.avatarImageView.mas_centerY).offset(SM_SIZE(10));
        make.height.equalTo(@(SM_SIZE(35)));
        make.width.equalTo(@(SM_SIZE(35)/38.f*29.f));
    }];

    _levelLabel = [UILabel new];
    _levelLabel.text = @"会员";
    _levelLabel.font = SM_CFont(29);
    _levelLabel.textColor = HEXCOLOR(@"FAE60A");
    [self addSubview:_levelLabel];
    [_levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_levelImageView.mas_right).offset(SM_SIZE(8));
        make.centerY.equalTo(_levelImageView.mas_centerY);
    }];

    _toLoginButton = [UIButton new];
    [_toLoginButton setTitle:@"去登录" forState:UIControlStateNormal];
    [_toLoginButton setTitleColor:SM_whiteColor forState:UIControlStateHighlighted];
    [_toLoginButton setTitleColor:SM_thumeColor forState:UIControlStateNormal];
    [_toLoginButton setBackgroundImage:[UIImage imageWithColor:SM_thumeColor] forState:UIControlStateHighlighted];
    [_toLoginButton setContentEdgeInsets:UIEdgeInsetsMake(SM_SIZE(10), SM_SIZE(20), SM_SIZE(10), SM_SIZE(20))];
    [_toLoginButton.titleLabel setFont:SM_CFont(32)];
    [_toLoginButton.layer setCornerRadius:SM_SIZE(10)];
    [_toLoginButton.layer setBorderColor:SM_thumeColor.CGColor];
    [_toLoginButton.layer setBorderWidth:1.f];
    [_toLoginButton setClipsToBounds:YES];
    [_toLoginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_toLoginButton];
    [_toLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-40)));
        make.centerY.equalTo(self.mas_centerY);
    }];
}


#pragma mark - Actions
- (void)loginButtonClick
{
    NaviPush(@"OMLoginViewController", (@{@"title":@"登录",@"isPresented":@(NO)}));
}


@end
